﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class modify : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {   
        LabelMsg.Visible = false;
        if (!Page.IsPostBack)
        {
            Calendar1.Visible = false;
            if (Session["user_id"] != null)
                ShowInfo();
            else
            {
                Response.Redirect("Default.aspx");
            }
        }
    }
    private void ShowInfo()
    {
        user user = (user)Session["user"];
        TextBoxName.Text = user.name;
        TextBoxIdcard.Text = user.idcard;
        TextBoxEmail.Text = user.email;
        TextBoxAddress.Text = user.address;
        TextBoxInfo.Text = user.userInfo;
        TextBoxTelephone.Text = user.telephone;
        TextBoxBirthday.Text = user.birthday.ToString();
        foreach (ListItem item in RadioButtonList1.Items)
            if (item.Value == user.sex)
                item.Selected = true;
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Calendar1.Visible = true;
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        TextBoxBirthday.Text = Calendar1.SelectedDate.ToString();
        if (!(TextBoxBirthday.Text == ""))
            Calendar1.Visible = false;
    }
    protected void ButtonModify_Click(object sender, EventArgs e)
    {
        if (Session["user_id"] != null)
        {
            if (TextBoxName.Text == "")
            {
                LabelMsg.Text = "姓名不能为空";
                LabelMsg.Visible = true;
                return;
            }
            user user = (user)Session["user"];
            SHA1CryptoServiceProvider sha1csp = new SHA1CryptoServiceProvider();
            byte[] src = System.Text.Encoding.UTF8.GetBytes(TextBoxPassword.Text);
            byte[] des = sha1csp.ComputeHash(src);//加密后的密码
            if (Convert.ToBase64String(des) == user.password)
            {
                user.name = TextBoxName.Text;
                user.idcard = TextBoxIdcard.Text;
                user.sex = RadioButtonList1.SelectedValue;
                user.email = TextBoxEmail.Text;
                user.address = TextBoxAddress.Text;
                user.userInfo = TextBoxInfo.Text;
                user.telephone = TextBoxTelephone.Text;
                if (TextBoxBirthday.Text != "")
                    user.birthday = DateTime.Parse(TextBoxBirthday.Text);          
                user.modify();
                Session["user"] = user;
                Response.AddHeader("Refresh", "0");
            }
            else
            {
                LabelMsg.Text = "密码错误,不允许修改";
                LabelMsg.Visible = true;
            }
        }
        else
        {
            LabelMsg.Text = "会话已过期，请重新登录";
            LabelMsg.Visible = true;
            Response.Redirect("Default.aspx");
        }
    }

}